/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.github.jinatonic.confetti.sample;

import com.github.jinatonic.confetti.CommonConfetti;
import com.github.jinatonic.confetti.ConfettiManager;
import com.github.jinatonic.confetti.ConfettiSource;
import ohos.aafwk.ability.Ability;
import ohos.aafwk.content.Intent;
import ohos.agp.components.AttrHelper;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.utils.Color;

import java.util.ArrayList;
import java.util.List;

public class FallingConfettiFromPointAbility extends Ability implements Component.ClickedListener {

    protected ComponentContainer container;

    protected int goldDark, goldMed, gold, goldLight;
    protected int[] colors;

    private final List<ConfettiManager> activeConfettiManagers = new ArrayList<>();

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_confetti);
        getWindow().setStatusBarColor(Color.getIntColor("#946335"));
        container = (ComponentContainer) findComponentById(ResourceTable.Id_container);
        findComponentById(ResourceTable.Id_generate_confetti_once_btn).setClickedListener(this);
        findComponentById(ResourceTable.Id_generate_confetti_stream_btn).setClickedListener(this);
        findComponentById(ResourceTable.Id_generate_confetti_infinite_btn).setClickedListener(this);
        findComponentById(ResourceTable.Id_generate_confetti_stop_btn).setClickedListener(this);
        findComponentById(ResourceTable.Id_back).setClickedListener(this);
        goldDark = Color.getIntColor("#946335");
        goldMed = Color.getIntColor("#ab804b");
        gold = Color.getIntColor("#cea774");
        goldLight = Color.getIntColor("#edd3b2");
        colors = new int[]{goldDark, goldMed, gold, goldLight};
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }

    private CommonConfetti getCommonConfetti() {
        final int size = AttrHelper.vp2px(6, container.getContext());
        final int velocitySlow = AttrHelper.vp2px(50, container.getContext());
        final int velocityNormal = AttrHelper.vp2px(100, container.getContext());
        final int velocityFast = AttrHelper.vp2px(200, container.getContext());

        final ConfettiSource confettiSource = new ConfettiSource(-velocitySlow, (int) (velocitySlow-size*2.5f));
        final CommonConfetti commonConfetti =
                CommonConfetti.rainingConfetti(container, confettiSource, colors);

        // Further configure it
        commonConfetti.getConfettiManager()
                .setVelocityX(velocityFast, velocitySlow)
                .setAccelerationX(-velocityNormal, velocitySlow)
                .setTargetVelocityX(0, velocitySlow / 2f)
                .setVelocityY(velocityNormal, velocitySlow);

        return commonConfetti;
    }

    @Override
    public void onClick(Component component) {
        switch (component.getId()) {
            case ResourceTable.Id_generate_confetti_once_btn:
                activeConfettiManagers.add(generateOnce());
                break;
            case ResourceTable.Id_generate_confetti_stream_btn:
                activeConfettiManagers.add(generateStream());
                break;
            case ResourceTable.Id_generate_confetti_infinite_btn:
                activeConfettiManagers.add(generateInfinite());
                break;
            case ResourceTable.Id_generate_confetti_stop_btn:
                for (ConfettiManager confettiManager : activeConfettiManagers) {
                    confettiManager.terminate();
                }
                activeConfettiManagers.clear();
                break;
            case ResourceTable.Id_back:
                terminateAbility();
                break;
        }
    }

    protected ConfettiManager generateOnce() {
        return getCommonConfetti().oneShot();
    }

    protected ConfettiManager generateStream() {
        return getCommonConfetti().stream(3000);
    }

    protected ConfettiManager generateInfinite() {
        return getCommonConfetti().infinite();
    }

    @Override
    protected void onBackPressed() {
        super.onBackPressed();
        for (ConfettiManager confettiManager : activeConfettiManagers) {
            confettiManager.terminate();
        }
        activeConfettiManagers.clear();
    }
}
